package com.zzyl.nursing.controller;

import java.util.List;
import java.util.Map;

import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.dto.DeviceDto;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 设备Controller
 *
 * @author ruoyi
 * @date 2025-09-17
 */
@RestController
@RequestMapping("/nursing/device")
@Api(tags = "设备相关接口")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;


    /**
     * 互联网平台同步产品列表
     *
     * @return
     */
    @PostMapping("/syncProductList")
    @ApiOperation(value = "同步产品列表")
    public R syncProductList() {
        this.deviceService.syncProductList();
        return R.ok();
    }

    /**
     * 查询所有产品列表
     *
     * @param device
     * @return
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "获取所有产品列表")
    public R<List<ProductVo>> allProduct() {
        List<ProductVo> list = deviceService.allProduct();
        return R.ok(list);
    }


    /**
     * 注册设备
     */
    @PostMapping("/register")
    @ApiOperation(value = "注册设备")
    public R registerDevice(@RequestBody DeviceDto deviceDto) {
        this.deviceService.registerDevice(deviceDto);
        return R.ok();
    }

    /**
     * 查询设备列表
     */
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取设备列表")
    public TableDataInfo<List<Device>> list(Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    /**
     * 查询设备详细数据
     * @param iotId
     * @return
     */
    @GetMapping("/{iotId}")
    @ApiOperation(value = "查询设备详细数据")
    public R<DeviceDetailVo> getDeviceById(@PathVariable String iotId) {
        DeviceDetailVo deviceById  = deviceService.getDeviceById(iotId);
        return R.ok(deviceById);
    }

    /**
     * 查看设备上报的数据
     * @param iotId
     * @return
     */
    @ApiOperation(value = "查看设备上报的数据")
    @GetMapping("/queryServiceProperties/{iotId}")
    public R<List<Map<String, Object>>> getInfo(@PathVariable("iotId") String iotId) {
        List<Map<String, Object>> ajaxResult = (List<Map<String, Object>>) deviceService.getInfo(iotId);
        return R.ok(ajaxResult);
    }

    /**
     * 修改
     * @param device
     * @return
     */
    @PutMapping
    public R addid(@RequestBody Device device) {
        deviceService.addid(device);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{iotId}")
      public R removeId(@PathVariable("iotId") String iotId) {
        deviceService.removeId(iotId);
        return R.ok();
    }


}
